package com.yt.app.api.action;

import com.yt.app.api.action.base.BaseAction;
import com.yt.app.api.util.Config;
import com.yt.app.api.util.PatternUtil;
import com.yt.app.api.util.UploadImageUtils;
import com.yt.app.api.vo.BaseVo;
import com.yt.app.api.vo.UserVo;
import com.yt.core.MsgBean;
import com.yt.util.IDCardCheck;
import com.yt.util.MD5Util;
import com.yt.website.api.interfaces.UserInfoService;
import com.yt.website.api.model.UserInfo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;

@Controller
@Api(tags = "07.个人中心")
@RequestMapping("/my/profile")
public class MyProfileController extends BaseAction{
    @Autowired
    private UserInfoService userInfoService;

    @ApiOperation(value="用户信息", httpMethod = "GET")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "用户信息", response = UserVo.class)})
    @RequestMapping("/get")
    public @ResponseBody  Object getProfile(
            @ApiIgnore BaseVo vo,
            @ApiIgnore HttpServletRequest request){
        MsgBean msgBean = new MsgBean();
        Long uid = BaseVo.getUIDFromHeader(request);
        UserInfo userInfo = userInfoService.get(uid);
        if(userInfo != null){
            msgBean.put("data", new UserVo(userInfo));
        }

        return msgBean.returnMsg();
    }

    @ApiOperation(value="更新信息", httpMethod = "POST")
    @RequestMapping("/update")
    public @ResponseBody  Object updateProfile(
            @ApiParam("用户昵称") @RequestParam(value = "nick", required = false, defaultValue = "0") String nick,
            @ApiParam("真实姓名") @RequestParam(value = "realName", required = false, defaultValue = "0") String realName,
            @ApiParam("性别") @RequestParam(value = "sex", required = false, defaultValue = "0") String sex,
            @ApiParam("身份证号") @RequestParam(value = "identity", required = false, defaultValue = "0") String identity,
            @ApiParam("手机号") @RequestParam(value = "mobile", required = false, defaultValue = "0") String mobile,
            @ApiIgnore BaseVo vo,
            @ApiIgnore HttpServletRequest request){
        MsgBean msgBean = new MsgBean();
//        if(!PatternUtil.USER_NAME_PATTERN.matcher(realName).matches()){
//            return msgBean.failure("姓名不正确").returnMsg();
//        }
        if(!IDCardCheck.validate(identity).isResult()){
            return msgBean.failure("身份证号不正确").returnMsg();
        }
        if(!PatternUtil.MOBILE_PATTERN.matcher(mobile).matches()){
            return msgBean.failure("手机号不正确").returnMsg();
        }
        UserInfo userInfo = userInfoService.get(BaseVo.getUIDFromHeader(request));
        if(userInfo != null){
            userInfo.setNick(nick);
            userInfo.setRealName(realName);
            userInfo.setIdentify(identity);
            userInfo.setMobile(mobile);
            userInfo.setSex(sex);
            userInfoService.update(userInfo);
        }else{
            msgBean.setFailure("1", "用户不存在");
        }

        return msgBean.returnMsg();
    }

    @ApiOperation(value="修改密码", httpMethod = "POST")
    @RequestMapping("/updatePassword")
    public @ResponseBody  Object updatePassword(
            @ApiParam("老密码") @RequestParam(value = "oldPwd", required = false, defaultValue = "0") String oldPwd,
            @ApiParam("新密码") @RequestParam(value = "newPwd", required = false, defaultValue = "0") String newPwd,
            @ApiIgnore BaseVo vo,
            @ApiIgnore HttpServletRequest request
    ){
        MsgBean msgBean = new MsgBean();
        UserInfo userInfo = userInfoService.get(BaseVo.getUIDFromHeader(request));
        if(userInfo == null){
            return msgBean.failure("用户不存在").returnMsg();
        }

        if(!userInfo.getPassword().equals(MD5Util.MD5(oldPwd))){
            return msgBean.failure("愿密码错误").returnMsg();
        }

        userInfo.setPassword(MD5Util.MD5(newPwd));
        userInfoService.update(userInfo);

        return msgBean.returnMsg();
    }

    @ApiOperation(value="上传头像", httpMethod = "POST")
    @RequestMapping("/uploadUserIcon")
    public @ResponseBody  Object uploadUserIcon(
            MultipartFile file,
            @ApiIgnore HttpServletRequest request
    ){
        MsgBean msgBean = new MsgBean();
        Long uid = BaseVo.getUIDFromHeader(request);
        String uri = UploadImageUtils.uploadImage(file, "userIcon", true);
        String fullUrl = Config.image_url + "/" + uri;
        msgBean.put("url", fullUrl);
        UserInfo userInfo = userInfoService.get(uid);
        userInfo.setUserIcon(fullUrl);
        userInfoService.update(userInfo);
        return msgBean.returnMsg();
    }
}
